/**
 * Created by Myron on 2016/1/30.
 */
//AngularJS 控制器




grApp.controller('DiyCartAngularCtrl', ['$scope',
    '$http','CartProductFactory',
    function ($scope, $http,cartProductFactory) {
        $scope.showRemindDiv = false; //显示购物车为空提醒
        $scope.showCartDiv = true; //显示购物车内容
        $scope.showActivityDiv = true; //显示购物车内容
        $scope.showMSotherActivityDiv = false; //显示满送活动额外提醒
        $scope.checkAllStatus = false; //全选按钮状态
        //数据变量
        $scope.GoodsPrice = 0.0;//货物总价 180
        $scope.DiscountPrice = 0.0; //优惠金额 30
        $scope.TotalPrice = 0.0; //购物车总价 150
        $scope.ActivityRuleMJArray = {};//满减活动规则列
        $scope.ActivityRuleMSArray = {};//满送活动规则列
        $scope.CartProductCount = 0;//购物车商品行数量
        $scope.CartProductArray = {};//购物车商品列表
        $scope.WaiMaiPrice=0;
        $scope.temp;

      //页面初始化载入顺序事件
        $scope.RunData = function () {

            var progress = $.AMUI.progress;
            var $modal = angular.element("#my-modal-loading");
            progress.start();
            $modal.modal();
            //页面初始化载入顺序事件
            //1清理购物车无效数据
            cartProductFactory.getProductList().then(function (rows) {
                $scope.CartProductCount = cartProductFactory.getRows();
                $scope.CartProductArray = cartProductFactory.getProductArray();
                if ($scope.CartProductCount == 0) {
                    $scope.showRemindDiv = true;
                    $scope.showCartDiv = false;
                }else{
                    //计算总价
                    for(var index in $scope.CartProductArray){
                        $scope.TotalPrice=$scope.TotalPrice+$scope.CartProductArray[index]['amountprice'];
                    }
                }
                progress.done();
                $modal.modal('close');
                console.log(5);
            });//5结束
        }
        //页面初始化
        $scope.RunData();


        //购物车商品删除按钮事件
        $scope.TrashBtn = function (index, product) {
            //$scope.CartProductArray.splice(index, 1);
            $scope.confirmname = product.name;
            $scope.temp=product;

            var $modal = angular.element("#my-confirm");
            $modal.modal({
                relatedTarget: this,
                onConfirm: function (options) {
                    //确定删除执行
                    cartProductFactory.trashProduct($scope.temp).then(function (data) {
                        if (data.code == 0) {
                            //成功
                            //$scope.RunData();//重新加载购物车数据
                            window.location.reload(); //删除成功后刷新页面。
                        } else {
                            alert('删除失败');
                            $modal.modal('close');
                            //失败
                        }
                    });
                },
                // closeOnConfirm: false,
                onCancel: function () {
                }
            });
        }

        //购物车结算按钮点击事件
        $scope.SubmitBtn = function () {

                window.location.href="/order/confirmOrder/diy";

        }
 }]);

//创建工厂/Service
//获取购物车商品数据
grApp.factory('CartProductFactory', ['$http', '$q', function ($http, $q) {
    var _code; //返回code状态
    var _rows; //购物车商品行数
    var _productArray; //购物车数据数组
    var service = {};
    //获取购物车商品数据
    service.getProductList = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'POST',
            url: '/extra/diy/cart/getProductListAjax'
        }).success(function (data, status, headers, config) {
            _code = data.code;
            _rows = data.rows;
            _productArray = data.ProductArray;
            deferred.resolve(data.rows);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //获取购物车商品行数
    service.getRows = function () {
        return _rows;
    }

    //获取购物车数据数组
    service.getProductArray = function () {
        return _productArray;
    }

    //删除商品
    service.trashProduct = function (Product) {

        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/diy/cart/delCartProductAjax',
            params: {CartProductId: Product.objectId}
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };

    return service;
}]);
